package com.mj.factory.totalSchuedule;

import com.mj.factory.processFlow.ProcessFlow;
import com.mj.factory.processFlow.ProcessFlowService;
import com.mj.factory.workOrder.WorkOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TotalScheduleService {
    @Autowired
    TotalScheduleDao totalScheduleDao;
    @Autowired
    ProcessFlowService processFlowService;

    public Page<TotalSchedule> findAll(Pageable pageable) {
        return totalScheduleDao.findAll(pageable);
    }
//
    public List<TotalSchedule> findAll() {
        return totalScheduleDao.findAll();
    }

    public TotalSchedule findOne(Long id) {
        return totalScheduleDao.findOne(id);
    }

    public void save(TotalSchedule totalSchedule) {
        totalScheduleDao.save(totalSchedule);
    }

    public TotalSchedule findByDrawingNumber(String drawingNumber) {
        return totalScheduleDao.findByDrawingNumber(drawingNumber);
    }
    public void delete(TotalSchedule totalSchedule){
        totalScheduleDao.delete(totalSchedule );
    }
//
    public boolean saveWithTechnologicalProcess(WorkOrder wo) {
        if (wo == null) return false;
        TotalSchedule totalSchedule = totalScheduleDao.findByDrawingNumber(wo.getChartNumber());
        totalSchedule = (null != totalSchedule) ? totalSchedule : new TotalSchedule();
        List<ProcessFlow> list = processFlowService.findByWorkOrderId(wo.getId());
        if (null == list) return false;
        if (list.size() <= 0) return false;
        float WorkTimes = 0;
        String OderNumber = wo.getOderNumber();
        for (ProcessFlow pf : list) {
            float workTime = pf.getEstimateTime();
            workTime = workTime * Integer.parseInt(OderNumber) ;
            WorkTimes += workTime;
            switch (pf.getProductionProcesses()) {
                case "锯床":
                    totalSchedule.setSawingMachine(workTime);
                    break;
                case "车床":
                    totalSchedule.setLathe(workTime);
                    break;
                case "铣床":
                    totalSchedule.setMillingMachine(workTime);
                    break;
                case "CNC":
                    totalSchedule.setcNC(workTime);
                    break;
                case "钻床":
                    totalSchedule.setDrillingMachine(workTime);
                    break;
                case "攻牙":
                    totalSchedule.setTapping(workTime);
                    break;
                case "线切割":
                    totalSchedule.setLineCutting(workTime);
                    break;
                case "焊接":
                    totalSchedule.setWelding(workTime);
                    break;
                case "打磨":
                    totalSchedule.setGrinding(workTime);
                    break;
                default:
                    break;
            }
        }
//            totalSchedule.setSerialNumber();
//            totalSchedule.setTextureOfMaterial(); //暂无法数据源 由他表提供
//            totalSchedule.setSpecificationOfParts();//暂无法数据源 由他表提供
//            totalSchedule.setCompany(); //单位暂不显示
//            totalSchedule.setPurchase(); 不使用
        totalSchedule.setProjectName(wo.getProductOrder().getProjectName());
        totalSchedule.setPartName(wo.getPartName());
        totalSchedule.setDrawingNumber(wo.getChartNumber());
        totalSchedule.setNumber(String.valueOf(wo.getOderNumber()));

        totalSchedule.setTotalWorkingHours(WorkTimes);
        totalSchedule.setRemainingWorkingHours(0);
        totalSchedule.setOutsourcingSurfaceTreatmentProcess("");
        totalSchedule.setProcessingState("");
        totalSchedule.setDegreeOfCompletion("");
        totalSchedule.setSeizedGoods("");
        totalSchedule.setPlannedDeliveryDate("");
        totalSchedule.setRemarks("");
        totalScheduleDao.save(totalSchedule);
        return false;
    }
//    public void  deleteTotalScheduleByDrawingNumber(String drawingNumber){
//        try {
//            totalScheduleDao.deleteTotalScheduleByDrawingNumber(drawingNumber);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//    }
}
